#!/usr/bin/env python
# $Id: addrinfo.py da23e41c6f10 2013/06/16 08:41:30 pgurumur $
# Copyright (c) 2013 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# File addrinfo.py generated on Monday June 10, 2013 @16:21:29 PDT
#
__version__  = "$Revision: da23e41c6f10 $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2013-06-16 08:41:30Z $"

import optparse
import os
import sys

from venya.lib import cmdparser, stdlib, ip, exception

class addrinfo(cmdparser.GetOpt):
   option_list = (
         optparse.make_option("-4", "--ipv4", action = "store_true",
            help = "Get information for an IPv4 address", default = False,
            dest = "ipv4"),
         optparse.make_option("-6", "--ipv6", action = "store_true",
            help = "Get information for an IPv6 address", default = False,
            dest = "ipv6"),
   ) + cmdparser.GetOpt.option_list

   help = """

   Not implemented

   """

   usage = "[ options ] address(es)"

   def __init__(self, **Keywords):
      super(stdlib.my_class(self), self).__init__(**Keywords)

   @cmdparser.getopts
   def handle(self):
      address = self.arguments[1:]
      if len(address) <= 0:
         self.error("%s: no ipv4/ipv6 address(es) given", str(self))

      for addr in address:
         temp = None
         try:
            temp = ip.ip(addr)
         except exception.IPError as err:
            self.warning("%s: %s", addr, err)
            continue

         addr = None
         if temp.version == 4:
            if temp.length < 32:
               addr = temp[0]
            else:
               addr = temp
         elif temp.version == 6:
            if temp.length < 128:
               addr = temp[0]
            else:
               addr = temp

if __name__ == "__main__":
   info = None
   try:
      info = addrinfo(version = 0.1, args = sys.argv)
      if info.length <= 1:
         info.printhelp()
      else:
         info.handle()
   except exception.GetOptError as err:
      print(err)
      sys.exit(1)
